import axios from 'axios';

const instance = axios.create({
  baseURL: 'http://localhost:3000',
  withCredentials: true,
  timeout: 3000,
});

// 添加请求拦截器
instance.interceptors.request.use(function(config){
  //config.headers['Content-Type'] = 'application/json';
   const token = localStorage.getItem('token')
   if (token) {
     config.headers.Authorization = `Bearer ${token}`
   }

  return config
},function(error){
  return Promise.reject(error)
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  console.log(error.message)
  //对服务端500得错误进行提示
  if (error.message == 'Network Error') {
      console.log('服务端错误，请稍后再试');
  }
  return Promise.reject(error);
 });

export default instance;